package com.bobo.Socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

public class ServerThread extends Thread{

	private Socket socket=null;
	
	public ServerThread(Socket socket){
		this.socket=socket;
	}
	@Override
	public void run() {
		try {
			InputStream is= socket.getInputStream();
			BufferedReader br=new BufferedReader(new InputStreamReader(is));
			String str="";
			StringBuffer sb=new StringBuffer();
			while((str=br.readLine())!=null){
				sb.append(str);
			}
			System.out.println("这是服务端，监听到客户端消息："+sb.toString());
			socket.shutdownInput();//关闭输入流
			
			//监听到消息之后给予客户端回应
		    OutputStream os= socket.getOutputStream();
		    os.write("您好，已经收到消息".getBytes());
		    os.flush();
		    socket.shutdownOutput();//关闭输出流
		    
		    //关闭资源
		    is.close();
		    br.close();
		    os.close();
		    socket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
}
